﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;

namespace TjZheng.Controls
{
    public class QueryInfo
    {
        /// <summary>
        /// 数据库类型枚举
        /// </summary>
        protected DataBaseType DataBaseType { get; set; }
        /// <summary>
        /// 当前查询条件转换
        /// </summary>
        protected virtual IQueryCondition QueryCondition { get; set; }
        /// <summary>
        /// Model前缀
        /// </summary>
        public string Prefix { get; set; }
        /// <summary>
        /// 查询字段
        /// </summary>
        public string QueryField { get; set; }
        /// <summary>
        /// 查询值
        /// </summary>
        public string QueryValue { get; set; }
        /// <summary>
        /// 查询类型
        /// </summary>
        public QueryDataType QueryDataType { get; set; }
        /// <summary>
        /// 查询方法
        /// </summary>
        public QueryMethod QueryMethod { get; set; }
        public QueryInfo()
            : this(string.Empty)
        {


        }
        public QueryInfo(string Prefix)
        {
            if (Prefix.IsNull())
            {
                this.Prefix = Guid.NewGuid().ToString().Replace("-", "");
            }
            else
            {
                this.Prefix = Prefix;
            }
            this.DataBaseType = DataBaseType.MsSql;
            this.QueryCondition = QueryConditionProvider.Factory[this.DataBaseType].GetQueryCondition();
        }
        public virtual void SetQueryCondition(DataBaseType DataBaseType)
        {
            this.DataBaseType = DataBaseType;
            this.QueryCondition = QueryConditionProvider.Factory[this.DataBaseType].GetQueryCondition();
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        public virtual string GetCondition()
        {
            string objCondition = string.Empty;
            switch (QueryMethod)
            {
                case QueryMethod.Equal:
                    objCondition = QueryCondition.EqualCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.NotEqual:
                    objCondition = QueryCondition.NotEqualCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.GreaterThan:
                    objCondition = QueryCondition.GreaterThanCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.LessThan:
                    objCondition = QueryCondition.LessThanCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.GreaterEqual:
                    objCondition = QueryCondition.GreaterEqualCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.LessEqual:
                    objCondition = QueryCondition.LessEqualCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.Contain:
                    objCondition = QueryCondition.ContainCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.StartWith:
                    objCondition = QueryCondition.StartWithCondition(QueryField, QueryValue, QueryDataType);
                    break;
                case QueryMethod.EndWith:
                    objCondition = QueryCondition.EndWithCondition(QueryField, QueryValue, QueryDataType);
                    break;
            }
            return objCondition;
        }
        /// <summary>
        /// 获取带参数查询条件
        /// </summary>
        /// <returns></returns>
        public virtual string GetConditionWithParameter(string ParameterName)
        {
            string objConditionWithParameters = string.Empty;
            switch (QueryMethod)
            {
                case QueryMethod.Equal:
                    objConditionWithParameters = QueryCondition.EqualConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.NotEqual:
                    objConditionWithParameters = QueryCondition.NotEqualConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.GreaterThan:
                    objConditionWithParameters = QueryCondition.GreaterThanConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.LessThan:
                    objConditionWithParameters = QueryCondition.LessThanConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.GreaterEqual:
                    objConditionWithParameters = QueryCondition.GreaterEqualConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.LessEqual:
                    objConditionWithParameters = QueryCondition.LessEqualConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.Contain:
                    objConditionWithParameters = QueryCondition.ContainConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.StartWith:
                    objConditionWithParameters = QueryCondition.StartWithConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
                case QueryMethod.EndWith:
                    objConditionWithParameters = QueryCondition.EndWithConditionWithParameters(QueryField, ParameterName, QueryDataType);
                    break;
            }
            return objConditionWithParameters;
        }
        /// <summary>
        /// 获取查询参数值
        /// </summary>
        /// <returns></returns>
        public virtual string GetParameterValue()
        {
            switch (QueryMethod)
            {
                case QueryMethod.Contain:
                    return this.QueryValue.FormatSelf("%{0}%");
                case QueryMethod.StartWith:
                    return this.QueryValue.FormatSelf("{0}%");
                case QueryMethod.EndWith:
                    return this.QueryValue.FormatSelf("%{0}");
                default:
                    return this.QueryValue;
            }

        }
        /// <summary>
        /// 将查询方法和查询类型转为Html代码
        /// </summary>
        /// <returns></returns>
        public virtual string QueryToHtml()
        {
            StringBuilder objStringBuilder = new StringBuilder();
            objStringBuilder.Append(QueryInputHidden("QueryInfo", "Prefixs", this.Prefix));
            objStringBuilder.Append(QueryInputHidden(this.Prefix, "QueryValue", this.QueryValue));
            objStringBuilder.Append(QueryInputHidden(this.Prefix, "QueryField", this.QueryField));
            objStringBuilder.Append(QueryInputHidden(this.Prefix, "QueryDataType", this.QueryDataType.ToString()));
            objStringBuilder.Append(QueryInputHidden(this.Prefix, "QueryMethod", this.QueryMethod.ToString()));
            return objStringBuilder.ToString();
        }

        protected string QueryInputHidden(string prefix, string name, string value)
        {
            return string.Format("<input name=\"{0}.{1}\" type=\"hidden\" value=\"{2}\" />", prefix, name, value);
        }
    }
}